using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

namespace WebControls
{
    public class SFCalendar : TextBox, IValidation, IReadOnly, ILabel, IClear
    {

        private DateTime _MinValue = DateTime.Parse("1/1/1900");
        private DateTime _MaxValue = DateTime.Parse("6/6/2079");

        public DateTime? Value
        {
            get
            {
                DateTime? dTime;

                if (this.Text == "")
                {
                    dTime = null;
                }
                else
                {
                    dTime = DateTime.Parse(this.Text);
                }
                return dTime;
            }

            set
            {
                if (value.Value.Year != 1)
                {

                    this.Text = value.Value.ToShortDateString();
                }
            }

        }

        protected override void OnLoad(EventArgs e)
        {
            List<SFLabel> lblRetval = StaticMethods.GetLabel(this);
            if (lblRetval != null)
            {
                foreach (SFLabel lbl in lblRetval)
                {
                    if (this.AllowNull)
                    {
                        lbl.CssClass = "cssLabelNormal";
                    }
                    else
                    {
                        lbl.CssClass = "cssLabelRequired";
                    }
                }
            }
            this.CssClass = "controlNormal";
            this.Attributes.Add("onfocus", "controlFocus(this)");
            this.Attributes.Add("onmouseout", "controlOnMouseOut(this)");
            this.Attributes.Add("onmouseover", "controlOnMouseOver(this)");
            this.Attributes.Add("onblur", "controlOnBlur(this)");
            this.AutoCompleteType = AutoCompleteType.Disabled;
            base.OnLoad(e);
        }

        #region IValidation Members
        private bool _AllowNull = false;
        public bool AllowNull
        {
            get
            {

                return this._AllowNull;
            }
            set
            {
                this._AllowNull = value;
            }
        }

        private bool _HasErrors = false;

        public bool HasErrors
        {
            get { return _HasErrors; }
        }

        private string _ErrorMessage = "";

        public string ErrorMessage
        {
            get
            {
                return _ErrorMessage;
            }
            set
            {
                _HasErrors = (value != "");
                _ErrorMessage = value;
            }
        }

        public bool Validate()
        {
            if (!AllowNull && this.Visible)
            {
                if (this.Text == "")
                {
                    
                    this.ErrorMessage += StaticMethods.GetErrorMessageMultilanguageString(13, new object[] { StaticMethods.GetErrorMessageControlName(this) });
                }
            }
            if (!this.HasErrors)
            {
                if (this.Visible)
                {
                    if (!(AllowNull && this.Text == ""))
                    {
                        if (!Validation.ValidateDateTime(this.Text))
                        {
                           
                            this.ErrorMessage += StaticMethods.GetErrorMessageMultilanguageString(14, new object[] { StaticMethods.GetErrorMessageControlName(this) });
                        }
                    }
                }
            }
            if (!this.HasErrors)
            {
                if (_MinValue != _MaxValue)
                {
                    if (this.Text != "")
                    {
                        if (!Validation.ValidateAmongDates(this.Text, _MinValue, _MaxValue))
                        {
                             this.ErrorMessage += StaticMethods.GetErrorMessageMultilanguageString(15, new object[] { StaticMethods.GetErrorMessageControlName(this), _MinValue.ToShortDateString(), _MaxValue.ToShortDateString() });
                        }
                    }
                }
            }
            return this.HasErrors;
        }

        #endregion

        #region IReadOnly Members

        public bool ReadOnly
        {
            get
            {
                return !this.Enabled;
            }
            set
            {
                this.Enabled = !value;
            }
        }

        #endregion

        #region ILabel Members

        private string _ControlsLabel;
        public string ControlsLabel
        {
            get
            {
                return _ControlsLabel;
            }
            set
            {
                _ControlsLabel = value;
            }
        }

        #endregion

        #region IClear Members

        public void Clear()
        {
            if (this.AllowNull)
            {
                this.Value = null;
                this.Text = null;

            }
            else
            {
                this.Value = DateTime.Now;

            }
        }

        #endregion
    }
}
